<?php


namespace app\common\cachemodel;


use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

/**
 * 商品模型
 * Class ZhunleGoods
 * @package app\common\cachemodel
 */
class MallGoods extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.mall_goods';
    protected $cacheModel = 'goods.mall_goods';

    /**
     * 获取不同类型下的商品ids
     * @param int $type
     * @return array|mixed
     * @throws \Exception
     */
    public function getGoodsList($type = 0)
    {
        $ids = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $type)->setSort('DESC')->ZsGetIocData('list', function () use ($type)
        {
            if ($type)
            {
                $where = ['status' => 1, 'tid' => $type];
            } else
            {
                $where = ['status' => 1];
            }
            try
            {
                $ids = $this->where($where)->order('sort desc')->column('sort', 'id');
            }
            catch (\Exception $e)
            {
                $ids = [];
            }
            return $ids;
        });
        return $ids;
    }

    /**
     * 获取分页的商品信息
     * @param int $type
     * @param array $field
     * @param int $limit
     * @return array
     * @throws \Exception
     */
    public function getGoodsPage($type = 0, $field = [], $limit = 10)
    {
        $page = input('page/d', 1);
        $ids = $this->getGoodsList($type);
        $num = count($ids);
        $data_list = getPaginatorData($num, $page, $limit);
        //获取分页后的数据
        $ids = array_slice($ids, $data_list['start'], $limit);
        unset($data_list['start'], $data_list['end']);
        $data_list['data'] = $this->batGetInfo($ids, $field);
        $data_list['ids'] = $ids;
        return $data_list;
    }

    /**
     * 获取商品信息
     * @param $ids
     * @param $field
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function batGetInfo($ids, $field = [])
    {
        return CacheManage::getInstance()->setCacheConfig($this->cacheModel)->batchGetCache($ids, function ($ids)
        {
            try
            {
                $data = $this->where(['id' => ['in', $ids]])->column('id,tid,sort,is_hot,is_top,name,description,imgs,or_price,now_price,vip_price,kd_price,faraway_kd_price,status,channel_id', 'id');
            }
            catch (\Exception $e)
            {
                $data = [];
            }
            return $data;
        }, $field);
    }


    /**
     * 获取商品图文信息
     * @param $id
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function getGoodsContent($id)
    {
        return CacheManage::getInstance()->setCacheConfig('goods.mall_goods_content', $id)->getDataAndSet(function () use ($id)
        {
            try
            {
                $info = $this->where(['id' => $id])->field('content')->findOrFail()->toArray();
                $content = $info ? $info['content'] : '';
            }
            catch (\Exception $e)
            {
                $content = '';
            }
            return $content;
        });
    }
}
